/**
 * Project Name:fastPlat
 * File Name:MemberRecordsController.java
 * Package Name:com.fast.cms.web.controller.member
 * Date:2016年12月01日 18时10分08秒
 * Copyright (c) 2016, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.controller.member;

import java.io.IOException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.page.QueryResult;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.member.MemberVisitRecords;
import com.fast.user.service.member.MemberRecordsService;

/**
 * ClassName:MemberRecordsController <br/>
 * 用途: MemberRecords web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2016年12月01日 18时10分08秒 <br/> 	 
 */
@Controller
@RequestMapping("/memberRecords")
public class MemberRecordsController extends BaseController{
	
	@Resource(name="memberRecordsService")
	private MemberRecordsService memberRecordsService;

	/**
	 * 编辑[MemberRecordsService]信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editMemberRecords.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑文件信息")
	public ResponseData editMemberRecordsService(){
		ResponseData responseData = new ResponseData();
		AdminUser curAdminUser = AdminCacheUtils.getLoginAdminUser();
		MemberVisitRecords memberRecords = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				memberRecords = this.memberRecordsService.getById(MemberVisitRecords.class,id);
				memberRecords = (MemberVisitRecords) webUtil.bindDataToBean(memberRecords);
			}else{
				memberRecords = new MemberVisitRecords();
				memberRecords = (MemberVisitRecords) webUtil.bindDataToBean(memberRecords);
				memberRecords.setCreater(curAdminUser.getUserName());
				memberRecords.setCreaterId(curAdminUser.getId());
				memberRecords.setCreateTime(new Date());
			}
			memberRecords.setEditorId(curAdminUser.getId());
			memberRecords.setEditorName(curAdminUser.getUserName());
			memberRecords.setEditTime(new Date());
			if(this.memberRecordsService.merge(memberRecordsService)!=null){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除[MemberRecords]信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemberRecordsByIdForHS.do")
	@AnoSysLog(actionName="删除文件到回收站")
	public ResponseData delMemberRecordsByIdForHS(int id) {
		LogUtils.info("delMemberRecordsByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(MemberVisitRecords.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = memberRecordsService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemberRecordsByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除[MemberRecords]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMemberRecordsById.do")
	@AnoSysLog(actionName="彻底删除某个文件")
	public ResponseData delMemberRecordsById(int id) {
		LogUtils.info("delMemberRecordsById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = memberRecordsService.deleteById(MemberVisitRecords.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delMemberRecordsById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个[MemberRecords]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelMemberRecords.do")
	@AnoSysLog(actionName="批量删除文章评论")
	public ResponseData batchDelMemberRecords(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = memberRecordsService.deleteByIds(MemberVisitRecords.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 得到[MemberRecords]列表
	 * @return PageView<MemberRecords>
	 * @throws IOException 
	 */
	@RequestMapping(value="/getMemberRecordsList.do", method=RequestMethod.POST)
	public PageView<MemberVisitRecords> getMemberRecordsList(HttpServletResponse response) throws IOException {
		int page = webUtil.getInt("page",1);//当前页
  		int pagesize = webUtil.getInt("pagesize",8);//每页面显示多少条
  		String articleFilesTypeId = webUtil.getString("articleFilesTypeId");
  		String deleteFlag = webUtil.getString("deleteFlag");
  		String status = webUtil.getString("status");
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if(!articleFilesTypeId.equals("")){
			sb.append("and memberRecordsServiceTypeId='"+articleFilesTypeId+"' ");
		}
		
		if(!deleteFlag.equals("")){
			sb.append("and delete_flag="+deleteFlag+" ");
		}else{
			//默认查询未回收的数据
			sb.append("and delete_flag=0 ");
		}
		
		if(!status.equals("")){
			sb.append("and status="+status+" ");
		}else{
			//默认查询
			sb.append("and status=1 ");
		}
		String ordersql = " order by edit_time desc ";
		QueryResult<MemberVisitRecords>  queryResult = this.memberRecordsService.queryForBeanListByPage(MemberVisitRecords.class,sb.toString(),ordersql,(page-1)*pagesize,pagesize);
		PageView<MemberVisitRecords> pageView = new PageView<MemberVisitRecords>();
	    pageView.setCurrentPage(page);
		pageView.setQueryResult(queryResult);
 		return pageView;
	}
	
	/**
	 * 查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getMemberRecordsDetail.do")
	public ModelAndView getMemberRecordsDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		MemberVisitRecords bean = memberRecordsService.queryForBeanById(MemberVisitRecords.class, id);
		try {
			mv.addObject("memberRecords",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/memberRecords/memberRecords-add.jsp");
		}else{
			mv.setViewName("/adminMain/memberRecords/memberRecords-detail.jsp");
		}
		return mv;
	}
}

